package com.boot.mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.mybatisplus.dao.BookMapper;
import com.boot.mybatisplus.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService{
   @Autowired
   BookMapper bookMapper;
    @Override
    public List<Book> searchBooks(Book book) {
//        QueryWrapper queryWrapper=new QueryWrapper();
//        if(book.getAuthor()!=null&&book.getAuthor()!=""){
//            queryWrapper.like("author",book.getAuthor());
//        }
//        if(book.getName()!=null&&book.getName()!=""){
//            queryWrapper.like("name",book.getName());
//        }
//        if (book.getCategory() != null && book.getCategory() != "") {
//            queryWrapper.eq("category",book.getCategory());
//        }
        LambdaQueryWrapper<Book> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(book.getAuthor()!=null&&book.getAuthor()!=""){
            lambdaQueryWrapper.like(Book::getAuthor,book.getAuthor());
        }
        if(book.getName()!=null&&book.getName()!=""){
            lambdaQueryWrapper.like(Book::getName,book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            lambdaQueryWrapper.eq(Book::getCategory,book.getCategory());
        }
        return bookMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage getPage(Integer pageNum) {
        IPage page=new Page(pageNum,3);
        return bookMapper.selectPage(page,null);
    }
}